package com.stedu.mall.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) throws SteduException {
        //根据用户名查询 判断用户是否存在
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new SteduException("该用户不存在");
        }
        //判断用户是否禁用或注销
        if (user.getStatus().equals(2) || user.getStatus().equals(3)) {
            throw new SteduException("该用户被禁用或不存在");
        }
        //对输入和密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        //和数据库中的密码对比
        if (!md5Pwd.equals(user.getPassword())) {
            throw new SteduException("密码错误，无法登录");
        }
        return user;
    }

    @Override
    public boolean reg(User user) throws SteduException {
        //判断该用户名在系统是否存在
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            throw new SteduException("该用户已经存在");
        }
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);
        //对密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(md5Pwd);
        user.setPayPassword(md5Pwd);
        //添加用户
        return userMapper.insert(user) == 1;
    }

    @Override
    public PageInfo<User> selectByAll(User user, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        //查询
        List<User> users = userMapper.selectByAll(user);

        //创建分页信息
        PageInfo<User> goodsPageInfo = new PageInfo<>(users);

        return goodsPageInfo;
    }

    @Override
    public boolean update(User user) {
       return userMapper.update(user) == 1;
    }

    @Override
    public boolean updateU(User user) throws SteduException {
        if (!user.getIdCard().matches("^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$")) {
            throw new SteduException("身份证格式不正确");
        }
        if (!user.getPhone().matches("^(?:(?:\\+|00)86)?1[3-9]\\d{9}$")) {
            throw new SteduException("电话格式不正确");
        }
        if (!user.getEmail().matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}")) {
            throw new SteduException("邮箱格式不正确");
        }
        user.setStatus(1);
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean uUpdate(User user) throws SteduException {
        if (user.getPhone() != null) {
            if (!user.getPhone().matches("^(?:(?:\\+|00)86)?1[3-9]\\d{9}$")) {
                throw new SteduException("电话格式不正确");
            }
        }
        if (user.getEmail() != null) {
            if (!user.getEmail().matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}")) {
                throw new SteduException("邮箱格式不正确");
            }
        }
        user.setStatus(1);
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean updateL(User user) {
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);
        //对密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(md5Pwd);

        return userMapper.update(user) == 1;
    }

    @Override
    public boolean updateP(User user) {
        //对密码加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPayPassword() + user.getSalt()));
        user.setPayPassword(md5Pwd);
        user.setPassword(null);
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean updateInsert(User user) {
        User user1 = userMapper.selectById(user.getId());
        //将余额加上充值的金额
        user1.setMoney(user1.getMoney().add(user.getMoney()));
        return userMapper.update(user1) == 1;
    }

    @Override
    public boolean reset(User user) {
        String substring = UUID.randomUUID().toString().substring(0, 8);
        //使用用户输入的原密码和盐加密 -> 加密之后用户输入的密码
        String userPassword = SecureUtil.md5(SecureUtil.md5(123 + substring));
        user.setPassword(userPassword);
        user.setSalt(substring);
        return userMapper.update(user) == 1;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User selectByUserName(String username) {
        User user = userMapper.selectByUsername(username);
        return user;
    }
}
